package com.example.demo.study.sceneQuestion.Q1.reader;

import com.example.demo.study.sceneQuestion.Q1.data.RawData;

import java.io.BufferedReader;
import java.io.FileReader;

// 文件数据读取实现类
public class FileDataReader implements DataReader {
    private final String filename;
    private BufferedReader reader;

    public FileDataReader(String filename) {
        this.filename = filename;
    }

    @Override
    public RawData read() throws Exception {
        if (reader == null) {
            reader = new BufferedReader(new FileReader(filename));
        }

        String line = reader.readLine();
        if (line == null) {
            reader.close();
            return null;
        }

        return new RawData(line, filename);
    }
}
